package view;

import controller.AccountValidation;
import model.ScanRead;

import java.io.IOException;

/**
 * description: 登录界面的类
 * author: 时间
 * time: 2022-01-10-11:46
 */
public class LoginView extends ViewRun implements Rules{
    public static String account;

    /**
     * Description: 登录/注册界面显示
     * Author: 时间
     * Date: 2022/1/10 12:32
     */
    @Override
    public void views() throws IOException {
        System.out.println("*************************登录/注册界面**************************");
        System.out.println("1-登录\t\t2-管理员登录\t\t3-注册\t\t4-忘记密码\t\t5-退出");
        System.out.println("***************************************************************");
        System.out.print("请选择：");
        char chr = ScanRead.readLogin();
        switch (chr){
            case '1':
                login();
                break;
            case '2':
                administratorLogin();
                break;
            case '3':
                registered();
                break;
            case '4':
                forgetPassword();
                break;
            case '5':
                exit();
                break;
        }
    }

    /**
     * Description: 管理员登录操作
     * Author: 时间
     * Date: 2022/1/11 11:06
     */
    private void administratorLogin(){
        // 每次输入前，重置flag1为true
        flag1 = true;
        while (flag1){
            System.out.print("请输入账号(-1退出)：");
            String account = ScanRead.readAccount();
            if (!exits(account)){break;}
            System.out.print("请输入密码(-1退出)：");
            String password = ScanRead.readPassword();
            if (!exits(password)){break;}
            boolean passwordEx = AccountValidation.passwordExist(account, password, pattern2);
            if (!passwordEx){
                System.out.println("密码或账号输入不正确，请重新输入，");
            } else {
                System.out.println("登录成功");
                flags = 2;
                break;
            }
        }
    }

    /**
     * Description: 注册相关操作
     * Author: 时间
     * Date: 2022/1/10 16:11
     */
    private void registered(){
        flag1 = true;
        while(flag1){
            System.out.print("请输入学号或工号(-1退出)：");
            account = ScanRead.readNumber();
            if (!AccountValidation.inferAccountExist(account)){
                System.out.println("该学号或工号不存在，请重新输入");
                continue;
            }
            if (!exits(account)){break;}
            System.out.print("请输入密码(-1退出)：");
            String password1 = ScanRead.readPassword();
            if (!exits(password1)){break;}
            System.out.print("请再次输入密码(-1退出)：");
            String password2 = ScanRead.readPassword();
            if (!exits(password2)){break;}
            if (!password1.equals(password2)){
                System.out.println("两次密码输入不一致，请重新输入");
                continue;
            }
            // 如果是true，已经存在
            boolean accountEx = AccountValidation.accountExist(account, pattern1);
            if (accountEx){
                System.out.println("该账号已经存在，请重新输入");
            }else{
                // 把信息写入文件
                operationFile.writeRegisteredFile(account + "-" + password1);
                System.out.println("注册成功");
                flags = 1;
                break;
            }
        }
    }

    /**
     * Description: 登录相关操作
     * Author: 时间
     * Date: 2022/1/10 16:12
     */
    private void login(){
        flag1 = true;
        while (flag1){
            System.out.print("请输入学号或工号(-1退出)：");
            account = ScanRead.readNumber();
            if(!exits(account)){break;}
            System.out.print("请输入密码(-1退出)：");
            String password = ScanRead.readPassword();
            if(!exits(password)){break;}
            boolean passwordEx = AccountValidation.passwordExist(account, password, pattern2);
            if (!passwordEx){
                System.out.println("密码或账号输入不正确，请重新输入");
            } else {
                System.out.println("登录成功");
                flags = 1;
                break;
            }
        }
    }

    private void forgetPassword() throws IOException {
        flag1 = true;
        while(flag1) {
            System.out.print("请输入学号或工号(-1退出)：");
            String account = ScanRead.readNumber();
            if (!exits(account)){break;}
            // 如果有该账号，才进行修改
            boolean accountEx = AccountValidation.accountExist(account, pattern1);
            if (accountEx) {
                System.out.print("请输入密码(-1退出)：");
                String password1 = ScanRead.readPassword();
                if (!exits(password1)){break;}
                System.out.print("请再次输入密码(-1退出)：");
                String password2 = ScanRead.readPassword();
                if (!exits(password2)){break;}
                if (!password1.equals(password2)) {
                    System.out.println("两次密码输入不一致，请重新输入");
                }else {
                    operationFile.changeRegisteredFile(account, password1);
                    System.out.println("修改成功");
                    break;
                }
            }else{
                System.out.println("该账号不存在");
            }
        }
    }

    /**
     * Description: 输入过程中检测是否有输入-1
     * Author: 时间
     * Date: 2022/1/12 12:03
     */
    private boolean exits(String str){
        if (str.equals("-1")){
            // 退出当前输入
            return flag1 = false;
        }
        return true;
    }

    /**
     * Description: 退出程序
     * Author: 时间
     * Date: 2022/1/11 10:54
     */
    @Override
    public void exit(){
        System.out.println("确定退出（y/n）：");
        char chr = ScanRead.readSelection();
        if (chr == 'Y'){
            flag = false;
        }
    }
}
